package org.tabletopmachine.states.preparation;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.tabletopmachine.Game;
import org.tabletopmachine.basics.Die;
import org.tabletopmachine.match.Player;
import org.tabletopmachine.twl.BasicTWLGameState;
import de.matthiasmann.twl.Alignment;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.ColumnLayout;
import de.matthiasmann.twl.ColumnLayout.Row;
import de.matthiasmann.twl.model.SimpleChangableListModel;
import de.matthiasmann.twl.ComboBox;

public class TopBottomChoiceState extends BasicTWLGameState {
	private static final int _id = 1;
	private final ColumnLayout _layout;
	private Die _p1die, _p2die;
	private Button _startButton;
	private Row rowPlayer1, rowPlayer2;
	private ComboBox<String> _topBottomBox;
	private boolean _addedBox;

	public TopBottomChoiceState() {
		_layout = new ColumnLayout();
		_startButton = new Button("Start");
		_startButton.setEnabled(false);
		_addedBox = false;
	}

	@Override
	public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {

	}

	@Override
	public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2) throws SlickException {

	}

	@Override
	public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
		if (_p1die.getValue() != null && (_p1die.getValue() == _p2die.getValue())) {
			_p1die.allowReRoll();
			_p2die.allowReRoll();
		} else if (!_p1die.isReRollAllowed() && !_p2die.isReRollAllowed() && !_addedBox && _p1die.getValue() != null && _p2die.getValue() != null
				&& (_p1die.getValue() != _p2die.getValue())) {
			_topBottomBox = new ComboBox<String>();
			SimpleChangableListModel<String> StringModel = new SimpleChangableListModel<String>();
			StringModel.addElement("Top");
			StringModel.addElement("Bottom");
			_topBottomBox.setModel(StringModel);

			if (_p1die.getValue() > _p2die.getValue()) {
				rowPlayer1.add(_topBottomBox);
			} else {
				rowPlayer2.add(_topBottomBox);
			}
			_addedBox = true;
		}
	}

	@Override
	public int getID() {
		return _id;
	}

	@Override
	public void enter(GameContainer container, StateBasedGame game) throws SlickException {
		super.enter(container, game);
		_p1die = new Die();
		_p2die = new Die();

		Player player1 = Game.getMatch().getPlayer1();
		Player player2 = Game.getMatch().getPlayer2();

		Row rowTitle = _layout.addRow("title");
		rowPlayer1 = _layout.addRow("col1", "col2", "col3");
		rowPlayer2 = _layout.addRow("col1", "col2", "col3");
		Row rowButton = _layout.addRow("col1", "col2");

		rowTitle.addLabel("Choose sides by dice");
		rowPlayer1.addLabel(player1.getName() + "(" + player1.getArmyName() + ")");
		rowPlayer1.add(_p1die, Alignment.RIGHT);
		rowPlayer2.addLabel(player2.getName() + "(" + player2.getArmyName() + ")");
		rowPlayer2.add(_p2die, Alignment.RIGHT);

		rowButton.addLabel("");
		rowButton.add(_startButton);
		getRootPane().add(_layout);
	}

	@Override
	protected void layoutRootPane() {
		super.layoutRootPane();
		_layout.adjustSize();
		_layout.setPosition(getRootPane().getInnerX() + (getRootPane().getInnerWidth() - _layout.getWidth()) / 2, getRootPane().getInnerY()
				+ (getRootPane().getInnerHeight() - _layout.getHeight()) / 2);
	}
}
